package com.hust360.percent.pojo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import android.app.Application;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.baidu.location.BDLocation.a;
import com.hust360.percent.PercentApplication;
import com.hust360.percent.utils.StorageUtils;

public class AreaDAOImpl implements AreaDAO {

	private PercentApplication application;

	public AreaDAOImpl(Application application) {
		this.application = (PercentApplication) application;
	}

	@Override
	public Area findById(Long id) {
		Area area = null;
		final SimpleDateFormat format = StorageUtils.simpleDateFormat;
		Cursor cursor = application.getLocalDB().query(Area.TABLE, null,
				Area.ID + "=" + id, null, null, null, null);
		if (cursor.moveToFirst()) {
			area = new Area();
			area.setId(cursor.getLong(cursor.getColumnIndex(Area.ID)));
			area.setImg(cursor.getString(cursor.getColumnIndex(Area.IMG)));
			area.setTitle(cursor.getString(cursor.getColumnIndex(Area.TITLE)));
			area.setType(cursor.getInt(cursor.getColumnIndex(Area.TYPE)));
			Date from = null;
			Date deadline = null;
			Date create = null;
			try {
				from = format.parse(cursor.getString(cursor
						.getColumnIndex(Area.FROM)));
			} catch (ParseException e) {
				e.printStackTrace();
			}

			try {
				deadline = format.parse(cursor.getString(cursor
						.getColumnIndex(Area.DEADLINE)));
			} catch (ParseException e) {
				e.printStackTrace();
			}

			try {
				create = format.parse(cursor.getString(cursor
						.getColumnIndex(Area.CREATE)));
			} catch (ParseException e) {
				e.printStackTrace();
			}

			area.setFromDate(from);
			area.setDeadline(deadline);
			area.setCreateDate(create);

			String listStr = cursor.getString(cursor
					.getColumnIndex(Area.SHOP_LIST));
			if (listStr != null) {
				String[] shopIds = listStr.split("<::>");
				if (shopIds != null) {
					ArrayList<Long> shopList = new ArrayList<Long>(
							shopIds.length);
					for (int i = 0; i < shopIds.length; i++) {
						shopList.add(Long.parseLong(shopIds[i]));
					}
					area.setShopList(shopList);
				}
			}

		}
		cursor.close();
		return area;
	}

	@Override
	public void deleteById(Long id) {
		application.getLocalDB().delete(Area.TABLE, Area.ID + "=" + id, null);
	}

	@Override
	public List<Area> findByArgs(String[] columns, String selection,
			String[] selectionArgs, String groupBy, String having,
			String orderBy) {
		final SimpleDateFormat format = StorageUtils.simpleDateFormat;
		Cursor c = application.getLocalDB().query(Area.TABLE, columns,
				selection, selectionArgs, groupBy, having, orderBy);

		ArrayList<Area> list = new ArrayList<Area>(c.getCount() + 5);
		Area area = null;
		while (c.moveToNext()) {
			area = new Area();

			area.setId(c.getLong(c.getColumnIndex(Area.ID)));
			area.setImg(c.getString(c.getColumnIndex(Area.IMG)));
			area.setTitle(c.getString(c.getColumnIndex(Area.TITLE)));
			area.setType(c.getInt(c.getColumnIndex(Area.TYPE)));
			Date from = null;
			Date deadline = null;
			Date create = null;
			try {
				from = format.parse(c.getString(c.getColumnIndex(Area.FROM)));
			} catch (ParseException e) {
				e.printStackTrace();
			}

			try {
				deadline = format.parse(c.getString(c
						.getColumnIndex(Area.DEADLINE)));
			} catch (ParseException e) {
				e.printStackTrace();
			}

			try {
				create = format
						.parse(c.getString(c.getColumnIndex(Area.CREATE)));
			} catch (ParseException e) {
				e.printStackTrace();
			}

			area.setFromDate(from);
			area.setDeadline(deadline);
			area.setCreateDate(create);

			String listStr = c.getString(c.getColumnIndex(Area.SHOP_LIST));
			if (listStr != null) {
				String[] shopIds = listStr.split("<::>");
				if (shopIds != null) {
					ArrayList<Long> shopList = new ArrayList<Long>(
							shopIds.length);
					for (int i = 0; i < shopIds.length; i++) {
						shopList.add(Long.parseLong(shopIds[i]));
					}
					area.setShopList(shopList);
				}
			}
			list.add(area);
		}
		c.close();
		return list;
	}

	@Override
	public void save(Area area) {
		ContentValues values = new ContentValues();
		values.put(Area.ID, area.getId());
		values.put(Area.IMG, area.getImg());
		values.put(Area.TITLE, area.getTitle());
		values.put(Area.CITY_ID, area.getCityId());
		values.put(Area.TYPE, area.getType());
		
		StringBuffer buffer = new StringBuffer();
		List<Long> list = area.getShopList();
		if (list != null) {
			Iterator<Long> iterator = list.iterator();
			while (iterator.hasNext()) {
				buffer.append(iterator.next() + "<::>");
			}
			values.put(Area.SHOP_LIST, buffer.toString());
		}
		application.getLocalDB().insertWithOnConflict(Area.TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
	}

}
